package com.roger.homecenterscreen.data.network

import com.roger.homecenterscreen.data.api.ApiService
import com.roger.homecenterscreen.data.api.WeatherApiService
import com.roger.homecenterscreen.data.api.CalendarApiService
import com.roger.homecenterscreen.data.api.LunarApiService
import com.roger.homecenterscreen.data.api.LocationApiService
import com.roger.homecenterscreen.data.api.CoordinateLocationApiService
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object NetworkModule {
    
    // 自定义内容API
    private const val CUSTOM_API_BASE_URL = "https://your-api-domain.com/"
    
    // 和风天气API
    private const val QWEATHER_BASE_URL = "https://devapi.qweather.com/"
    
    // 聚合数据API - 万年历使用v.juhe.cn域名
    private const val JUHE_BASE_URL = "http://v.juhe.cn/"
    private const val JUHE_OTHER_BASE_URL = "http://apis.juhe.cn/"
    
    // 免费农历API - 使用开源项目提供的接口
    private const val LUNAR_BASE_URL = "https://api.yuanfei.site/"
    
    private val loggingInterceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }
    
    private val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(loggingInterceptor)
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .build()
    
    // 自定义内容API服务
    private val customApiRetrofit = Retrofit.Builder()
        .baseUrl(CUSTOM_API_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    
    // 和风天气API服务
    private val qweatherRetrofit = Retrofit.Builder()
        .baseUrl(QWEATHER_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    
    // 聚合数据API服务 - 万年历API
    private val juheRetrofit = Retrofit.Builder()
        .baseUrl(JUHE_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    
    // 聚合数据其他API服务 (如IP查询)
    private val juheOtherRetrofit = Retrofit.Builder()
        .baseUrl(JUHE_OTHER_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    
    // 免费农历API服务
    private val lunarRetrofit = Retrofit.Builder()
        .baseUrl(LUNAR_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    
    val apiService: ApiService = customApiRetrofit.create(ApiService::class.java)
    val weatherApiService: WeatherApiService = qweatherRetrofit.create(WeatherApiService::class.java)
    val calendarApiService: CalendarApiService = juheRetrofit.create(CalendarApiService::class.java)
    val lunarApiService: LunarApiService = lunarRetrofit.create(LunarApiService::class.java)
    val locationApiService: LocationApiService = juheOtherRetrofit.create(LocationApiService::class.java)
    
    // 经纬度转地址API (cn.apihz.cn)
    private val coordinateRetrofit = Retrofit.Builder()
        .baseUrl("https://cn.apihz.cn/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    
    val coordinateLocationApiService: CoordinateLocationApiService = coordinateRetrofit.create(CoordinateLocationApiService::class.java)
    
    // IP地址转位置API (聚合数据)
    val ipLocationApiService: LocationApiService = juheRetrofit.create(LocationApiService::class.java)
} 